<?php

namespace App\Http\Controllers;

use App\Models\Zhanghu;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Zz extends Controller
{
    //
    public function index(){
       $user = session('user');
       $data = Zhanghu::with('uname',$user)->first();
        return [
            'code'=>200,
            'msg'=>'查询成功',
            'data'=>$data,
        ];
    }
    public function zz(){
        $data = \request()->post();
        $model = Zhanghu::where('zhm',$data['zhm'])->first();
        $user = session('user');
        $uname = Zhanghu::with('user',$user)->first();
        if (empty($model)){
            return [
                'code'=>3,
                'msg'=>'请输入正确的卡号',
                'data'=>[]
            ];
        }
        if ($model['user']!=$data['uname']){
            return [
                'code'=>4,
                'msg'=>'卡号和姓名不一致',
                'data'=>[]
            ];
        }
        if ($uname['ye']<$data['je']){
            return [
                'code'=>5,
                'msg'=>'转账记录不能大于用户余额',
                'data'=>[]
            ];
        }
        if ($uname['type']==1){
            return [
                'code'=>6,
                'msg'=>'用户已冻结不能进行转账',
                'data'=>[]
            ];
        }
        if ($data['je'] < $model['ye']){
            return [
                'code'=>7,
                'msg'=>'转账金额不能大于用户余额',
                'data'=>[]
            ];
        }
        if ($data['je']==null && $data['je']< 0){
            return [
                'code'=>7,
                'msg'=>'转账金额不能为空和为负数',
                'data'=>[]
            ];
        }
        DB::transaction(function () {
            try {



                DB::update('update users set votes = 1');
            }catch (\Exception $e){
                DB::delete('delete from posts');
            }



        });
    }
}
